package com.joywifi.android.demo.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.joywifi.android.demo.R;

import butterknife.Bind;
import butterknife.ButterKnife;

public class MainButtomTabFragment extends Fragment {

    private View mView;
    private ViewPager mViewPager;
    private TabLayout mTabLayout;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.fragement_main_buttom_tab_pager, container, false);
        mViewPager = (ViewPager) mView.findViewById(R.id.viewPager);
        mTabLayout = (TabLayout) mView.findViewById(R.id.tabLayout);

        ButtomTabFragmentPagerAdapter pagerAdapter =
        new ButtomTabFragmentPagerAdapter(getChildFragmentManager(), getActivity());

        mViewPager.setAdapter(pagerAdapter);
        mTabLayout.setupWithViewPager(mViewPager);

        for (int i = 0; i < mTabLayout.getTabCount(); i++) {
        TabLayout.Tab tab = mTabLayout.getTabAt(i);
        if (tab != null) {
            tab.setCustomView(pagerAdapter.getTabView(i));
        }
    }
        return mView;
    }

    public class ButtomTabFragmentPagerAdapter extends FragmentPagerAdapter {

        final int PAGE_COUNT = 3;
        private String tabTitles[] = new String[]{"TAB1", "TAB2", "TAB3"};
        private Context context;

        public ButtomTabFragmentPagerAdapter(FragmentManager fm, Context context) {
        super(fm);
        this.context = context;
    }

        public View getTabView(int position) {
            View v = LayoutInflater.from(context).inflate(R.layout.custom_buttom_tab, null);
            TextView tv = (TextView) v.findViewById(R.id.textView);
            tv.setText(tabTitles[position]);
            return v;
        }

        @Override
        public Fragment getItem(int position) {
            return ButtomTabPageFragment.newInstance(position + 1);
        }

        @Override
        public int getCount() {
            return PAGE_COUNT;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return tabTitles[position];
        }
    }

    public static class ButtomTabPageFragment extends Fragment {
        public static final String ARG_PAGE = "ARG_PAGE";
        @Bind(R.id.id_textview)
        TextView textView;
        private int mPage;

        public static ButtomTabPageFragment newInstance(int page) {
            Bundle args = new Bundle();
            args.putInt(ARG_PAGE, page);
            ButtomTabPageFragment fragment = new ButtomTabPageFragment();
            fragment.setArguments(args);
            return fragment;
        }

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            mPage = getArguments().getInt(ARG_PAGE);
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.fragment_tab_text_item, container, false);
            ButterKnife.bind(this, view);
            textView.setText("Fragment #" + mPage);
            return view;
        }
    }
}
